//
//  OrderViewController.m
//  Butler
//
//  Created by liaozhenming on 16/6/12.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "OrderViewController.h"

#import "GoodsCollectionViewCell.h"
#import "GoodsTableViewCell.h"
#import "KKCustomerSelectedView.h"
#import "KKCustomerEditView.h"
#import "KKEmployeeSelectedView.h"

@interface OrderViewController ()

@property (nonatomic, strong) NSMutableArray *selectedGoodsArray;

@property (nonatomic, strong) NSArray *categoryItems;

@property (nonatomic, strong) NSArray *allGoodsItems;

@property (nonatomic, strong) KKCustomerSelectedView *customerSelectedView;
@property (nonatomic, strong) KKCustomerEditView *customerEditView;
@property (nonatomic, strong) KKEmployeeSelectedView *employeeSelectedView;

@property (nonatomic, strong) KKCategoryItem *selectedCategoryItem;

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _userInfoTable.tableFooterView = [UIView new];
    _categoryTable.tableFooterView = [UIView new];
    
    NSMutableArray *categoryItems = [NSMutableArray arrayWithArray: [[KKButlerDataManager kk_manager] arr_categoryItems]];
    
    {
        KKCategoryItem *allCategoryItem = [KKCategoryItem new];
        allCategoryItem.kk_name = @"全部分类";
        [categoryItems insertObject: allCategoryItem atIndex: 0];
        _selectedCategoryItem = allCategoryItem;
    }
    _categoryItems = [NSArray arrayWithArray: categoryItems];
    [self update_goodsPageItemsListWithCategoryId: nil];
    _selectedGoodsArray = [NSMutableArray array];
    
    _customerSelectedView = [[KKCustomerSelectedView alloc] init];
    _customerEditView = [[KKCustomerEditView alloc] init];
    _employeeSelectedView = [[KKEmployeeSelectedView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods

- (void)update_goodsPageItemsListWithCategoryId:(NSString *)categoryId {
    
    __weak UICollectionView *weakCollectionView = _goodsCollectionView;
    [[KKButlerDataManager kk_manager] kk_searchAllGoodsItemsWithCategoryId: categoryId result:^(id object) {
        if (object != nil) {
            _allGoodsItems = [NSArray arrayWithArray: object];
            [weakCollectionView reloadData];
        }
    }];
}

#pragma mark - Action methods

- (IBAction)action_back:(id)sender {
    
    [self.navigationController popViewControllerAnimated: YES];
}

- (IBAction)action_login:(id)sender {
    [_customerSelectedView kk_show];
}

- (IBAction)action_register:(id)sender {
    [_customerEditView kk_show];
}

- (IBAction)action_reset:(id)sender {
    
    [_selectedGoodsArray removeAllObjects];
    [_userInfoTable reloadData];
}

- (IBAction)action_sure:(id)sender {
    
    [self.navigationController popViewControllerAnimated: YES];
}

- (IBAction)action_employee:(id)sender {
    [_employeeSelectedView kk_show];
}

#pragma mark - UITableView methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == _userInfoTable) {
        return _selectedGoodsArray.count;
    }
    else {
        return _categoryItems.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _userInfoTable) {
        GoodsTableViewCell *cell = [GoodsTableViewCell kk_cellWithTableView: tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else {
        UITableViewCell *cell = [UITableViewCell kk_cellWithTableView: tableView];
        KKCategoryItem *item = _categoryItems[indexPath.row];
        cell.textLabel.text = item.kk_name;
        return cell;
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView != _userInfoTable)
        return UITableViewCellEditingStyleNone;
    else {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView != _userInfoTable) {
        KKCategoryItem *item = _categoryItems[indexPath.row];
        _selectedCategoryItem = item;
        [self update_goodsPageItemsListWithCategoryId: item.kk_id];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        KKGoodsItem *selectedItem = [_selectedGoodsArray objectAtIndex: indexPath.row];
        [_selectedGoodsArray removeObject: selectedItem];
        [tableView deleteRowsAtIndexPaths: @[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark - UICollection methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _allGoodsItems.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(160, 100);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 20, 20, 20);
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GoodsCollectionViewCell *cell = [GoodsCollectionViewCell kk_cellWithCollectionView: collectionView indexPath: indexPath];
    cell.backgroundColor = [UIColor kk_colorWithHexString: @"f0f0f0"];
    
    KKGoodsItem *item = [_allGoodsItems objectAtIndex: indexPath.row];
    cell.nameLabel.text = [item.kk_name kk_stringValue];
    cell.priceLabel.text = [NSString stringWithFormat: @"¥%@",[item.kk_price kk_stringValue]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    KKGoodsItem *selectedItem = [_allGoodsItems objectAtIndex: indexPath.row];
    
    for (KKGoodsItem *item in _selectedGoodsArray) {
        if ([item.kk_id isEqualToString: selectedItem.kk_id]) {
            return;
        }
    }
    
    [_selectedGoodsArray addObject: selectedItem];
    [_userInfoTable reloadData];
}

@end
